package com.czl.module_user.fragment

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.module_user.BR
import com.czl.module_user.R
import com.czl.module_user.adapter.IntegralAdapter
import com.czl.module_user.viewmodel.IntegralListViewModel
import com.czl.module_user.databinding.FragmentIntegralListBinding

/**
 *
 * @Description:积分记录
 * @Author: XCH
 * @CreateDate: 2022/1/6 11:08
 */
@Route(path = AppConstants.Router.User.F_INTEGRAL_LIST)
class IntegralListFragment : BaseFragment<FragmentIntegralListBinding, IntegralListViewModel>() {

    private lateinit var mAdapter: IntegralAdapter

    override fun initContentView(): Int {
        return R.layout.fragment_integral_list
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("积分记录")
        initAdapter()
        reload()
    }

    private fun initAdapter() {
        mAdapter = IntegralAdapter()
        binding.ryCommon.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        viewModel.uc.refreshListEvent.observe(this, {
            binding.smartCommon.autoRefresh()
        })
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            handleRecyclerviewData(
                data == null,
                data?.records as MutableList<*>?,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon,
                viewModel.currentPage,
                data?.size == data?.pages
            )
        })
    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }
}